<template>
    <div class="analysis-result">
      <el-card>
        <!-- 搜索栏 -->
        <el-form :inline="true" class="search-form">
          <el-form-item label="用户名">
            <el-input v-model="searchParams.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="分析日期">
            <el-date-picker
              v-model="searchParams.analysisDate"
              type="date"
              placeholder="选择日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchResults">搜索</el-button>
          </el-form-item>
        </el-form>
  
        <!-- 分析结果列表 -->
        <el-table :data="resultList" style="width: 100%" class="result-table" v-loading="loading">
          <el-table-column prop="userName" label="用户名"></el-table-column>
          <el-table-column prop="healthStatus" label="健康状态"></el-table-column>
          <el-table-column prop="analysisDate" label="分析日期"></el-table-column>
          <el-table-column prop="suggestions" label="健康建议"></el-table-column>
        </el-table>
  
        <!-- 分页 -->
        <el-pagination
          class="pagination"
          :current-page="currentPage"
          :page-size="pageSize"
          :total="total"
          @current-change="handlePageChange"
        ></el-pagination>
      </el-card>
    </div>
  </template>
  
  <script>
  import { getAnalysisResultList } from '@/api/analysisResult'
  
  export default {
    data() {
      return {
        searchParams: { username: '', analysisDate: '' },
        resultList: [],
        currentPage: 1,
        pageSize: 10,
        total: 0,
        loading: false
      }
    },
    created() {
      this.fetchResults()
    },
    methods: {
      async fetchResults() {
        this.loading = true
        try {
          const response = await getAnalysisResultList({
            page: this.currentPage,
            size: this.pageSize,
            ...this.searchParams
          })
          this.resultList = response.data.records
          this.total = response.total
        } catch (error) {
          this.$message.error('获取分析结果列表失败')
        } finally {
          this.loading = false
        }
      },
      searchResults() {
        this.currentPage = 1
        this.fetchResults()
      },
      handlePageChange(page) {
        this.currentPage = page
        this.fetchResults()
      }
    }
  }
  </script>
  <style scoped>
  .analysis-result {
    padding: 20px;
  }
  .search-form {
    margin-bottom: 20px;
  }
  .result-table {
    margin-top: 20px;
  }
  .pagination {
    margin-top: 20px;
    text-align: right;
  }
  </style>